package Export;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JFileChooser;

import DataModel.Feature;
import Viewer.FileChooserX;

/**
 * This class is for the export of a gtf file.<br>
 * <a href="http://genome.ucsc.edu/FAQ/FAQformat.html#format4">http://genome.ucsc.edu/FAQ/FAQformat.html#format4</a>
 * @author Stefan Lorenz<br>
 * <br>26.11.2012
 */
public class ExportGtf {

    /**
     * List of Features to export
     */
    private ArrayList<Feature> list;
    /**
     * Filechooser
     */
    private FileChooserX chooserX;

    /**
     * Constructor of the gtf exporter
     * @param list list of {@link Feature}s to export
     * @param chooserX filechooser
     */
    public ExportGtf(ArrayList<Feature> list, FileChooserX chooserX) {
	this.chooserX = chooserX;
	this.list = list;
	try {
	    exportGtf();
	} catch (IOException e) {
	    e.printStackTrace();
	}
    }

    /**
     * Chooses file to save and executes the write function
     * @throws IOException
     */
    private void exportGtf() throws IOException {
	if (this.chooserX.showSaveDialog(this.chooserX) == JFileChooser.APPROVE_OPTION) {
	    File file = this.chooserX.getSelectedFile();
	    if (!(file.getName().endsWith(".gtf")))
		file = new File(file.getAbsolutePath() + ".gtf");

	    BufferedWriter writer = new BufferedWriter(new FileWriter(file));

	    for (int i = 0; i < this.list.size(); i++) {
		writeGtf(writer, this.list.get(i));
	    }
	    writer.flush();
	    writer.close();
	}
    }

    /**
     * Writes a feature in gtf format. Separated into type 'exon','transcript' or 'gene' and 'CDS'
     * @param writer writer to write
     * @param f feature to export
     * @throws IOException
     */
    private void writeGtf(BufferedWriter writer, Feature f) throws IOException {
	if(f.getType().equals("mRNA")){
	    for (int i = 0; i < f.getListOfPositions().size(); i++) {
		writer.write(f.getLocus()+"\tGeneScapes\texon\t"+f.getListOfPositions().get(i).getStart()+"\t"+
			f.getListOfPositions().get(i).getStop()+"\t.\t"+f.getStrand()+"\t.\t" +
					"gene_id \""+f.getName()+"\"; transcript_id \""+f.getName()+"\";");
		writer.newLine();
		
	    }
	    writer.flush();
	}
	else if(f.getType().equals("CDS")){
	    for (int i = 0; i < f.getListOfPositions().size(); i++) {
		writer.write(f.getLocus()+"\tGeneScapes\tCDS\t"+f.getListOfPositions().get(i).getStart()+"\t"+
			f.getListOfPositions().get(i).getStop()+"\t.\t"+f.getStrand()+"\t.\t" +
					"gene_id \""+f.getName()+"\"; transcript_id \""+f.getName()+"\";");
		writer.newLine();
		
	    }
	    writer.flush();
	}
	else if(f.getType().equals("gene") || f.getType().equals("transcript")){
	    for (int i = 0; i < f.getListOfPositions().size(); i++) {
		writer.write(f.getLocus()+"\tGeneScapes\ttranscript\t"+f.getListOfPositions().get(i).getStart()+"\t"+
			f.getListOfPositions().get(i).getStop()+"\t.\t"+f.getStrand()+"\t.\t" +
					"gene_id \""+f.getName()+"\"; transcript_id \""+f.getName()+"\";");
		writer.newLine();
		
	    }
	    writer.flush();
	}
    }
}
